var columns = [
    {
        field: 'index',
        title: '序号',
        align:"center",
        formatter:function(value, row, index){
            return index + 1;
        }
    },{
        field: 'parentId',
        align: 'center',
        title: '家长名称'
    },{
        field: 'orgId',
        align: 'center',
        title: '学生组织机构'
    },{
        field: 'subOrgId',
        align: 'center',
        title: '组织机构'
    },{
        field: 'name',
        align: 'center',
        title: '学生姓名'
    },{
        field: 'lessonAccount',
        align: 'center',
        title: '课时数'
    },{
        field: 'sex',
        title: '用户性别',
        align: 'center',
        formatter: function (value, row, index) {
            return pubForm.getSex(row.sex);
        }
    },{
        field: 'createTime',
        align: 'center',
        title: '创建时间'
    }
    // ,{
    //     field: 'state',
    //     title: '是否启用',
    //     align: 'center',
    //     formatter: function (value, row, index) {
    //         return pubSwitch.getSwitchBtnCss(row);
    //     }
    // },{
    //     field: 'operator',
    //     title: '操作',
    //     align: 'center',
    //     formatter: function (value, row, index) {
    //         console.log("aaaa");
    //         return pubOptBtn.getOptBtnCustom2('updateUser',menuSub,row.orgId,row.parentId,row.id);
    //     }
    // }
    ];
var updateUser={
    updateUserInit:function () {
        var selDom = $("#duty");
        if('0' == type){
            selDom.append("<option value='master'>总校校长</option>");
            selDom.append("<option value='branch'>分校校长</option>");
            selDom.append("<option value='teacher'>教师</option>");
            selDom.append("<option value='student'>学生</option>");
            $("#topPurchaseNum").show();
        }
        if('1' == type && '1' == stype && '1' == ctype){  //登录人总校长，点击菜单也是总校长
            selDom.append("<option value='master'>总校校长</option>");
        }
        if('1' == type && '1' == stype && '2' == ctype){  //登录人总校长，点击菜单也是分校长
            selDom.append("<option value='branch'>分校校长</option>");
        }
        if('1' == type && '2' == stype){  //登录人总校长，点击菜单也是老师管理
            selDom.append("<option value='branch'>分校校长</option>");
            selDom.append("<option value='teacher'>教师</option>");
        }
        if('1' == type && '3' == stype){  //登录人总校长，点击菜单也是学生管理
            selDom.append("<option value='branch'>分校校长</option>");
            selDom.append("<option value='teacher'>教师</option>");
            selDom.append("<option value='student'>学生</option>");
            $("#topPurchaseNum").show();
        }

        /**登录人事分校长的情况*/
        if('2' == type && '1' == stype && '1' == ctype){ //登录人是分校长，点击菜单也是校长
            selDom.append("<option value='master'>总校校长</option>");
        }
        if('2' == type && '1' == stype && '2' == ctype){ //登录人是分校长，点击菜单也是校长
            selDom.append("<option value='branch'>分校校长</option>");
        }
        if('2' == type && '2' == stype){ //登录人是分校长，点击菜单也是老师管理
            selDom.append("<option value='teacher'>教师</option>");
        }
        if('2' == type && '3' == stype){ //登录人是分校长，点击菜单也是学生管理
            selDom.append("<option value='teacher'>教师</option>");
            selDom.append("<option value='student'>学生</option>");
            $("#topPurchaseNum").show();
        }
        /**登录人事分校长的情况*/

        /**登录人事老师的情况*/
        if('3' == type && '1' == stype && '1' == ctype){ //登录人是老师，点击菜单也是总校长管理
            selDom.append("<option value='teacher'>总校校长</option>");
        }
        if('3' == type && '1' == stype && '2' == ctype){ //登录人是老师，点击菜单也是总校长管理
            selDom.append("<option value='branch'>分校校长</option>");
        }
        if('3' == type && '2' == stype){ //登录人是老师，点击菜单也是老师管理
            selDom.append("<option value='teacher'>教师</option>");
        }
        if('3' == type && '3' == stype){ //登录人是老师，点击菜单也是学生管理
            selDom.append("<option value='student'>学生</option>");
        }
        /**登录人事老师的情况*/
        if('4' == type){ //学生不可以登录，这里登录人事学生情况不存在
            selDom.append("<option value='student'>学生</option>");
        }
        sessionStorage.removeItem("orgId",orgId);
        sessionStorage.removeItem("orgName",orgName);
        $.ajax({
            url:application.contextPath+"/user/queryUserById",
            dataType:"json",
            type:"POST",
            data:{
                "userId":userId
            },
            success:function (res) {
                var data=res.data;
                $("#ObjId").val(userId);
                $("#loginName").val(data.loginName);
                // $("#loginpwd").val(data.loginPwd);
                $("#trueName").val(data.trueName);
                data.sex =="1" ? $("input[type='radio'][name='sex']").eq(0).attr("checked",true) : $("input[type='radio'][name='sex']").eq(1).attr("checked",true);
                $("#qqCode").val(data.qqCode);
                $("#qqName").val(data.qqName);
                $("#wechatCode").val(data.wechatCode);
                $("#wechatName").val(data.wechatName);
                $("#tel").val(data.tel);
                $("#email").val(data.email);
                $("#photo").val(data.photo);
                $("#orgId").val(data.orgId);
                $("#orgName").val(data.orgName);
                $("#cityName").find("option[value='"+data.cityName+"']").attr("selected",true);
                $("#duty").find("option[value='"+data.duty+"']").attr("selected",true);
                if(null != data.userOrgAccount && '' != data.userOrgAccount){
                    if("" != data.userOrgAccount.lessonAccount && null != data.userOrgAccount.lessonAccount){
                        $("#purchaseNum").val(data.userOrgAccount.lessonAccount);
                        $("#userCount").val(data.userOrgAccount.id);
                    }else {
                        $("#purchaseNum").val("0");
                        $("#userCount").val("null");
                    }
                }else {
                    $("#purchaseNum").val("0");
                    $("#userCount").val("null");
                }
                if(!jsEcc.isNull(data.orgId)){
                    $("#getUserCount").hide();
                }
            }
        });
        $("#cancel").click(function(){
            loadUtil = new Load($);
            loadUtil.load({
                container:'#homeContent',
                url: application.contextPath+"/user/showUserPage?menuId=4&stype="+stype+"&type="+type
            });
            return false;
        });
        $("#duty").change(function () {
            var duty= $(this).children('option:selected').val();
            if("master" == duty){
                $("#getOrgList").hide();
            }else {
                $("#getOrgList").show();
            }
        });

        pubForm.getBootTableSwitch("#tableList","/user/queryUserSubById","/base/switch/updateState",userId,"t_user_sub");
    },
    showUpdateUserOrgAccountPage:function(id){
        loadUtil = new Load($);
        loadUtil.load({
            container:'#homeContent',
            url: application.contextPath+"/user/showUpdateUserOrgAccountPage?orgId="+id.split(",")[0]+"&parentId="+id.split(",")[1]+"&type="+type+"&userId="+userId
        });
    },
    showUserOrgAccountPage:function(id){
        loadUtil = new Load($);
        loadUtil.load({
            container:'#homeContent',
            url: application.contextPath+"/user/showUserOrgAccountPage?orgId="+id.split(",")[0]+"&parentId="+id.split(",")[1]
        });
    },
    delUserOrgAccount:function(id){
        pubForm.delRow(id.split(",")[2],"/user/delUserOrgAccount","/user/queryUserSubById","#tableList");
    },
    queryOrg:function () {
        $.get(
            application.contextPath+"/user/showUserOrgPage?userId="+userId,
            function (res) {
                layer.open({
                    type:1,
                    title:'添加组织机构',
                    content:res,
                    maxmin:true,//最大，最小开关
                    shade:0.5,//遮盖层模糊度
                    anim:1,//打开跟关闭动画
                    scrollbar:true,//是否设置滚轮
                    area:["1080px","500px"]
                })
            }
        )
    },
    commitUpdate:function (form) {
        var data = {};
        var array = $(form).serializeArray();
        for(var index in array){
            data[array[index]['name']] = array[index]['value']
        }
        if(jsEcc.isNull(data.qqCode)){
            if(!jsEcc.isNumber(data.qqCode)){
                layer.msg('QQ号格式不正确',{icon:5});
                return false;
            }
        }
        if(jsEcc.isNull(data.tel)){
            if(!jsEcc.isTel(data.tel)){
                layer.msg('手机号码格式不正确',{icon:5});
                return false;
            }
        }
        if(data.duty != "master"){
            if(!jsEcc.isNull(data.orgName)){
                layer.msg('组织结构请选择',{icon:5});
                return false;
            }
        }else {
            data.duty="";
        }
        data.id=userId;
        data.type=type;
        data.userCountId= $("#userCount").val();
        $.ajax({
            url: application.contextPath+"/user/updateUserByUserId",
            type:"POST",
            contentType: "application/json; charset=utf-8",
            dataType:"json",
            data:JSON.stringify({parmsMap:data}),
            success:function (res) {
                if(res.isSuccess){
                    layer.msg('用户修改成功', {
                        icon: 1, title: '修改提示', btn: ['是'], yes() {
                            layer.close(layer.index);
                            loadUtil = new Load($);
                            loadUtil.load({
                                container: '#homeContent',
                                url: application.contextPath + backUrl
                            });
                        }
                    });
                }else {
                    layer.open({
                        title:"加载失败!",
                        content:'页面加载失败!'
                    });
                }
            }
        });
        return false;
    },
    updateUserDescInit:function () {
        $.ajax({
            url:application.contextPath+"/user/queryUserDescAllByUserId",
            dataType:"json",
            type:"GET",
            async:false,
            data:{
                "userId":userId
            },
            success:function (res) {
                var imgs=[];
                $.each(res.data.userProUrlList,function(index,item) {
                    imgs.push(item);
                });
                //初始化图片展示
                $("#case1").attr("data-value", imgs);

                $("#case1").upload(
                    function(_this,data){
                        layer.msg("图片链接："+data,{time: 5000},function(){
                        });
                    }
                );
                if(res.data.userProfile != null && res.data.userProfile != ''){
                    tinyMCE.activeEditor.setContent(res.data.userProfile)
                }

            }
        });
    },
    saveUserDesc:function () {
        var data={};
        data.id=$("#ObjId").val();
        data.userDesc=tinyMCE.editors[0].getContent();
        $.ajax({
            url: application.contextPath+"/user/updateUserDesc",
            type:"POST",
            contentType: "application/json; charset=utf-8",
            dataType:"json",
            data:JSON.stringify({user:data}),
            success:function (res) {
                if(res.isSuccess){
                    layer.msg('用户修改成功', {
                        icon: 1, title: '修改提示', btn: ['是'], yes() {
                            layer.close(layer.index);
                            loadUtil = new Load($);
                            loadUtil.load({
                                container: '#homeContent',
                                url: application.contextPath + backUrl
                            });
                        }
                    });
                }else {
                    layer.open({
                        title:"加载失败!",
                        content:'页面加载失败!'
                    });
                }
            }
        });
    },
    queryUserCountByUserId(){
        if($("#duty").val()!="student"){
            layer.msg("无法对该职位用户设置已购买课时",{icon:5});
        }else {
            $.get(
                application.contextPath+"/user/queryUserCountByUserId/"+$("#userCount").val()+"/"+$("#purchaseNum").val()+"/"+userId,
                function (res) {
                    layer.open({
                        type:1,
                        title:'修改课时数',
                        content:res,
                        maxmin:true,//最大，最小开关
                        shade:0.5,//遮盖层模糊度
                        anim:1,//打开跟关闭动画
                        scrollbar:true,//是否设置滚轮
                        area:["1080px","500px"]
                    })
                }
            )
        }
    }
};


$(document).ready(function(){
    updateUser.updateUserInit();
    tinymce.init({
        convert_urls: false,
        selector: 'textarea.tinymce',
        height: 300,
        plugins: [
            'advlist autolink lists link image charmap print preview anchor',
            'searchreplace visualblocks code fullscreen',
            'insertdatetime media table contextmenu paste code'
        ],
        toolbar: 'insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image',
        init_instance_callback:updateUser.updateUserDescInit
    });
    // $("#topPurchaseNum").mouseover(function () {
    //     layer.msg("222");
    // });
    // $("#topPurchaseNum").mouseout(function () {
    //     layer.close(layer.index);
    // });
});